﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FmcWebsite.Core;
using ContentManagementAdministrator.Dialogs;

namespace ContentManagementAdministrator
{
    public partial class UserControlArticles : UserControl, ITab
    {
        private IEnumerable<Article> articles;

        public UserControlArticles()
        {
            InitializeComponent();
            articles = Article.GetAll();
            
            FillList(articles.OrderBy(y => -y.Id));

            // setting the search event
            var key_change = Observable
                                .FromEvent<EventArgs>(textBoxSearch, "TextChanged")
                                .Select(x => (x.Sender as TextBox).Text)
                                .Throttle(TimeSpan.FromMilliseconds(400))
                                .Select(x => x.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                                .ObserveOnDispatcher();

            
            key_change.Subscribe(
                    x => 
                        {
                            if (x.Count() == 0)
                                FillList(articles.OrderBy(y => -y.Id));
                            else
                                FillList(articles.Reverse().Where(y => y.SearchHit(x) > 0).OrderBy(y => -y.SearchHit(x)));
                        }
                    );
            
            FillList(articles.OrderBy(x => -x.Id));
        }

        private void FillList(IEnumerable<Article> list)
        {
            listView1.BeginUpdate();
            try
            {
                listView1.Items.Clear();
                foreach (Article a in list)
                {
                    var item = listView1.Items.Add("");
                    item.Text = a.Id.ToString();
                    item.SubItems.Add(a.InsertDate.ToString());
                    item.SubItems.Add(a.Category.Name);
                    item.SubItems.Add(Multilang.Extract(a.Author.Name));
                    item.SubItems.Add((new Multilang(a.Title)).GetAvailable());
                    item.SubItems.Add(a.Publish ? "Yes" : "No");
                    item.Tag = a;                    
                }
            }
            finally
            {
                listView1.EndUpdate();
            }
        }

        #region ITab Members

        public string GetTitle()
        {
            return "Articles";
        }

        #endregion

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxSearch.Text = "";
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Article art = (Article)listView1.SelectedItems[0].Tag;
            FormArticleEdit.Edit(art);
            UpdateList(art);
        }

        private void UpdateList(Article a)
        {
            var items = listView1.Items.Cast<ListViewItem>().Where(x => x.Tag == a);
            if (items.Count() > 0)
            {
                var item = items.First();
                item.SubItems.Clear();
                item.SubItems.Add(a.InsertDate.ToString());
                item.SubItems.Add(a.Category.Name);
                item.SubItems.Add(Multilang.Extract(a.Author.Name));
                item.SubItems.Add((new Multilang(a.Title)).GetAvailable());
                item.SubItems.Add(a.Publish ? "Yes" : "No");
                item.Tag = a;
                item.Text = a.Id.ToString();
            }
            else
            {
                listView1.Items.Add("").Tag = a;
                UpdateList(a);
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listView1.SelectedItems)
                {
                    Article art = (Article)item.Tag;
                    art.Publish = false;
                    art.Update();
                    UpdateList(art);
                }
            }
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            Article art = new Article();
            if (FormArticleEdit.Edit(art) == DialogResult.OK)
            {
                UpdateList(art);
            }
        }

        private void buttonPublish_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                Article art = (Article)item.Tag;
                art.Publish = true;
                art.Update();
                UpdateList(art);
            }
        }

        private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            Color bg = Color.White;
            if (!((Article)e.Item.Tag).Publish)
            {
                bg = Color.Pink;
            }
            if (listView1.SelectedIndices.Contains(e.ItemIndex))
            {
                bg = Color.SkyBlue;
            }
            e.Graphics.FillRectangle(new SolidBrush(bg), e.Bounds);
            e.DrawText();            
        }

        private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {            
            e.DrawDefault = true;
        }

        private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawText();            
        }

    }
}
